<?php

//pour detecter le numagance, on cherche le premier espace
$pos = strpos($_POST['agence'], " ");
//on recupere le numagence
$numagence = substr($_POST['agence'],0,$pos);
//on recupere le nom de l'agence
$agence = substr($_POST['agence'],($pos+3),strlen($_POST['agence']));
if ($numagence=="Selectionnez") {$numagence=0;} //si la personne n'a pas selectionner d'agence dans la liste


if ($_POST['identifiant'] == "" || $_POST['password'] == "") //si champ vide => erreur
    {
	echo("<br><br><br><br>Veuillez remplir tout les champs requis!<br><br><br><br>");
    }
else
    {
$query="SELECT connexion('".$_POST['identifiant']."','".$_POST['password']."',".$numagence.") AS DROITS FROM DUAL";
if ($sql->execute($query)=="nok")
{
	echo "Erreur lors de l'execution de la requete. ";
}
else
{
$_SESSION['droits']=$sql->recup(0,"DROITS");
$_SESSION['login']=$_POST['identifiant'];
$_SESSION['numagence']=$numagence;
$_SESSION['agence']=$agence;

$personne="SELECT DISTINCT p.numPersonne FROM Personne p, Client c, Employe e WHERE (p.numPersonne=c.numPersonne AND c.loginClient='".$_SESSION['login']."') OR (p.numPersonne=e.numPersonne AND e.loginEmploye='".$_SESSION['login']."')";
$sql->execute($personne);
$_SESSION['numpers']=$sql->recup(0,"NUMPERSONNE");

        if ($_SESSION['droits'] == 0)
            {
            echo "<br><br><br><br>Erreur, verifier votre agence, login, mot de passe.<br><br><br><br>";
            }
        elseif ($_SESSION['droits'] == 1)
                   {
                   echo "
                   <script language='javascript' type='text/javascript'>
                   window.location.replace('index.php?p=acceuil_client');
                   </script>
                   ";
                   }
        elseif ($_SESSION['droits'] == 2)
                  {
                  echo "
                   <script language='javascript' type='text/javascript'>
                   window.location.replace('index.php?p=acceuil_employe');
                   </script>
                   ";
                  }
        elseif ($_SESSION['droits'] == 3)
               {
               echo "
                   <script language='javascript' type='text/javascript'>
                   window.location.replace('index.php?p=acceuil_admin');
                   </script>
                   ";
               }
    }
}

?>